<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>city</title>
</head>
<body>
    

<script src="data/area-vue.js"></script>
<script src="data/alphabetCity.js"></script>
<script src="data/only_city.js"></script>
<script src="http://cache.weikuw.com/tpl/static/lib/plugIn/pinyin/pinyin_dict_notone.js"></script>
<script src="http://cache.weikuw.com/tpl/static/lib/plugIn/pinyin/pinyin_dict_withtone.js"></script>
<script src="http://cache.weikuw.com/tpl/static/lib/plugIn/pinyin/pinyinUtil.js"></script>
<script>
    var cityDataPivot = {
        data: [],
        cityList: {},
        alphabetCity: {},
        alpArr: [],
        init(){
            console.log('City Data Pivot:', this);

            for(var i in area_vue[86]){
                let item = area_vue[86][i];
                this.data.push({
                    text: item,
                    value: i,
                    children: []
                })
            }
            console.log('data:', this.data);
        },
        getCity(area){
            let num = 0;
            this.data.map((item, key) => {
                let list = area_vue[item.value];
                for(var i in list){
                    let val = list[i],
                        param = {
                            text: val,
                            value: i
                        };
                    num++;
                    if(area) param = area(param);
                    this.data[key].children.push(param);
                }
            })
            // console.log('data city('+num+'):', JSON.stringify(this.data));
        },
        getArea(street){
            this.getCity(item => {
                item.children = [];
                let list = area_vue[item.value];
                // console.log('item:', item);
                for(var i in list){
                    let val = list[i],
                        param = {
                            text: val,
                            value: i
                        };
                    if(street) param = street(param);
                    item.children.push(param)
                }
                return item;
            });
        },
        getStreet(){
            this.getArea(item => {
                item.children = [];
                let list = area_vue[item.value];
                // console.log('item:', item);
                for(var i in list){
                    let val = list[i],
                        param = {
                            text: val,
                            value: i
                        };
                    item.children.push(param)
                }
                return item;
            })
        },
        getAlphabetSortData(){
            let n = 0, cityListTemp = {};
            for(var i in cityList){
                let item = cityList[i],
                    data = {
                        text: item,
                        value: i,
                        pinyin: pinyinUtil.getPinyin(item, ' ', false)
                    },
                    alp = data.pinyin.substr(0, 1).toUpperCase();
                if(!cityListTemp[alp]) cityListTemp[alp] = [], this.alpArr.push(alp);
                cityListTemp[alp].push(data);
                n++;
            }
            this.alpArr = this.bubbleSort(this.alpArr);
            this.alpArr.map(alp => {
                this.cityList[alp] = cityListTemp[alp];
            })
            console.log(`cityList(${n}):`, this.cityList);
            // console.log(JSON.stringify(this.cityList));
        },
        bubbleSort(arr){
            let len = arr.length;
            if(len <= 0) return false;
            for(var i = 0; i < len - 1; i++){
                for(var j = 0; j < len - i - 1; j++){
                    let next = arr[j + 1];
                    if(arr[j].charCodeAt() > next.charCodeAt()){
                        arr[j + 1] = arr[j];
                        arr[j] = next;
                    }
                }
            }
            return arr;
        }
    };
    cityDataPivot.init();
</script>
</body>
</html>